package com.lbs.jiepiaisi.view;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.activity.VisitTypeActivity;
import com.lbs.jiepiaisi.bean.AnswerQuestionBean;
import com.lbs.jiepiaisi.utils.ButtonUtil;
import com.lbs.jiepiaisi.utils.NetUtil;

/**
 * @author ydm
 * @date 2018/2/23
 * @desc
 */

public class HomeSingleView extends LinearLayout {


    private Context mContext;

    private TextView mTitle;
    private RadioGroup  mSel;
    private EditText mDes;

    private String question;
    private String yDes;
    private String nDes;

    private int position;

    private AnswerQuestionBean.Result result;

    public HomeSingleView(Context context) {
        super(context);
        initView(context);
    }

    public HomeSingleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HomeSingleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        this.mContext = context;
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.view_home_question_single, this);

        // 获取控件
        mTitle = (TextView) findViewById(R.id.tv_title);

        mSel = (RadioGroup)findViewById(R.id.rg_sel);

        mDes = (EditText)findViewById(R.id.et_des);

    }

    private String answer = "";
    private String answerDes;
    private String answerValue = "";
    public void setTitle(AnswerQuestionBean.Result result,String question, final String yDes, final String nDes,int position){
        this.question = question;
        this.yDes = yDes;
        this.nDes = nDes;
        this.result = result;
        this.position = position;
        mTitle.setText(question);
        mSel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId == R.id.rb_yes){
                    answer = "1";
                    if(yDes != null && !"".equals(yDes)){
                        mDes.setHint(yDes);
                        mDes.setVisibility(VISIBLE);

                    }else {
                        mDes.setVisibility(GONE);
                    }

                }else if(checkedId == R.id.rb_no){
                    answer = "0";
                    if(nDes != null && !"".equals(nDes)){
                        mDes.setHint(nDes);
                        mDes.setVisibility(VISIBLE);
                    }else {
                        mDes.setVisibility(GONE);
                    }

                }

            }
        });
    }

    public AnswerQuestionBean.Result getResult() {
        return result;
    }

    public int getPosition() {
        return position;
    }

    public String  getAnswer(){
        return answer;
    }
    public String getAnswerDes(){
        if("1".equals(answer)){
            if(yDes != null && !"".equals(yDes)){
                answerValue = mDes.getText().toString();
                answerDes = yDes + answerValue;
            }else {
                answerDes =  "";
            }

        }else if("0".equals(answer)){
            if(nDes != null && !"".equals(nDes)){
                answerValue = mDes.getText().toString();;
                answerDes = nDes + answerValue;
            }else {
                answerDes = "";
            }

        }
        return answerDes;
    }

    public boolean isSel() {
        if("".equals(answer)){
            return false;
        }
        answerValue = mDes.getText().toString();
        if("1".equals(answer)){
            if(mDes.getVisibility() != GONE){
                if("".equals(answerValue)){
                    return false;
                }
            }

        }else if("0".equals(answer)){
            if(mDes.getVisibility() != GONE){
                if("".equals(answerValue)){
                    return false;
                }
            }

        }
        return true;
    }
}
